import { createSingle } from "./utils";

function contentToUrl(content: string) {
  const blob = new Blob([content], {
    type: "application/javascript; charset=utf-8",
  });
  const blobURL = URL.createObjectURL(blob);
  return blobURL;
}

function creteNodeHook({ onRequest, onResponse }: CreateHookProps) {
  // const headAppendChild = HTMLHeadElement.prototype.appendChild;
  // const divAppendChild = HTMLDivElement.prototype.appendChild;
  function rewriteNodeAppendChild(Element: any) {
    const rawAppendChild = Element.prototype.appendChild;

    Element.prototype.appendChild = function (node: any) {

     const resource =   node.nodeName.toLowerCase()
     // 不是脚本标签 直接返回
      if(!['script','link'].includes(resource)){
        console.log('不是脚本直接返回')
        return rawAppendChild.call(this, node);
      }
      let src = node.src || node.href; 
      
      if(node.innerHTML||!src||!['script'].includes(resource)){
        return rawAppendChild.call(this, node);
      }
      if (src.startsWith("blob:http")) {
        return rawAppendChild.call(this, node);
      }
      const ctx = {
        url: src,
        method: "GET",
        resource: node.nodeName.toLowerCase(),
        headers: {},
        resBody: "",
        data: {},
        setHeader: (name: string, value: any) => {
          // console.warn(node.nodeName, "不支持设置header");
        },
      };
      if (onRequest) {
        onRequest(ctx, {
          next(ctx: ContextProps) {
            
            // if (onResponse && ctx.resource === "script") {
            //   const url  = node.src;
            //   Object.defineProperty(node, "src", {
            //     get() {
            //       return null;
            //     },
            //   });
            //   (window as any)
            //     .__fetch(url)
            //     .then((res: { text: () => any; }) => res.text())
            //     .then((resBody: ResBodyType) => {
            //       ctx.resBody = resBody;
            //       onResponse(ctx, {
            //         send(resBody: ResBodyType) {
            //           const url = contentToUrl(resBody);
            //           const script = document.createElement("script");
            //           script.src = url;
            //           document.head.appendChild(script);

            //         },
            //         next(ctx) {
            //           console.log("重新加载");
            //           const url = contentToUrl(
            //             resBody 
            //           );
            //           const script = document.createElement("script");
            //           script.src = url;
            //           document.head.appendChild(script);
            //         },
            //       });
            //     });

             
            // } else {
              if (node.src) {
                node.src = ctx.url;
              } else if (node.href) {
                node.href = ctx.url;
              }
            // }
          },
          send(resBody) {
            console.log("发送");
            const url = contentToUrl(resBody);
            if (node.src) {
              node.src = url;
            } else if (node.href) {
              node.href = url;
            }
          },
        });
      }
      

      // if (node.src||node.href) {
console.log('直接传家',rawAppendChild,node)
        return rawAppendChild.call(this, node);
      // }
     
      // return false;
    };
  }  

   console.log('new ddd')
  // HTMLDivElement.prototype.appendChild
  // rewriteNodeAppendChild(HTMLDivElement);
  // rewriteNodeAppendChild(HTMLHeadElement);
}
export default  creteNodeHook;
